﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace CoolCode.Controls
{
    public partial class Window : IWindow
    {

        public UserControl Content { get; set; }
        public string Title { get; set; }

        public double Height { get; set; }
        public double Width { get; set; }

        public Window()
        {
        }

        #region IWindow Members

        public void Show()
        {
            Popup popup = new Popup();
            //popup.Name = "pop"+;
            Border placeHolder = new Border();
            UserControl rootPage = Application.Current.RootVisual as UserControl;
            double w = rootPage.ActualWidth;
            double h = rootPage.ActualHeight;
            placeHolder = new Border
            {
                Name = commonDialog,
                Background = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100)),
                Opacity = 0.8,
                Width = w,
                Height = h,
            };

            //DialogPanel d = new DialogPanel
            //{
            //    //Height = Height,
            //    //Width = Width,
            //    Title = this.Title,
            //    DragEnable = true,

            //};
            Dialog d = new Dialog(this.Title, this.Content as IDialog); 
          //  d.Content = this.Content;

            //d.Dispatcher.BeginInvoke(() =>
            //{
            //    UserControl  content = d.Content as UserControl ;
            //    if (content != null)
            //    {
            //        if (d.Height == 0)
            //            d.Height = content.ActualHeight;
            //        if (d.Width == 0)
            //            d.Width = content.ActualWidth;
            //    }

            //});

           // d.OnClose = (s, e) => { placeHolder.Visibility = Visibility.Collapsed; popup.IsOpen = false; };
            //d.HorizontalContentAlignment = HorizontalAlignment.Center;
            d.HorizontalAlignment = HorizontalAlignment.Center;
            //d.VerticalContentAlignment = VerticalAlignment.Center;
            d.VerticalAlignment = VerticalAlignment.Center;
            //Grid g = new Grid();
            //g.Children.Add(d);
            //placeHolder.HorizontalAlignment = HorizontalAlignment.Center;
            //placeHolder.VerticalAlignment = VerticalAlignment.Center;
            placeHolder.Child =  d;
            placeHolder.Visibility = Visibility.Visible;
            popup.Child = placeHolder;
            popup.IsOpen = true;
        }

        public void ShowModal()
        {
            Show();
        }

        public void Close()
        {
            //close_Click(   , new  RoutedEventArgs());
        }

        public void Hide()
        {
            throw new NotImplementedException();
        }

        #endregion


        void close_Click(object sender, RoutedEventArgs e)
        {
            if (this.OnClose != null)
                OnClose(sender, e);
        }


        public RoutedEventHandler OnClose { get; set; }

    }
}
